
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.forum;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oxygen.util.StringUtils;
import oxygen.web.ServletInteractionContext;
import oxygen.web.ViewContext;
import oxygen.web.WebUtils;

public class ForumServletInteractionContext extends ServletInteractionContext {
  //url = ctxPath + prefix + / + action [+ / + id]
  public ForumServletInteractionContext(ServletContext sctx, HttpServletRequest request, HttpServletResponse response) throws Exception {
    super(sctx, request, response);
  }
  
  public ViewContext toViewContext() throws Exception {
    String s = null;
    Long id = null;
    String action = null;
    if((s = request.getParameter("action")) != null) { 
      action = s;
    }
    if((s = request.getParameter("id")) != null) {
      id = getLong(s);
    }      
    ViewContext vctx = new ViewContext();
    String path = request.getRequestURI();
    //System.out.println("request uri: " + path);
    //path = URLDecoder.decode(path, "UTF-8");
    path = StringUtils.decodeURLEncodedPercentHexHex(path);
    String ctxpath = request.getContextPath();
    path = path.substring(ctxpath.length());
    String urlprefix = ForumLocal.getForumEngine().getProperty(ForumConstants.SERVLET_MAPPING_PREFIX_KEY);
    path = path.substring(urlprefix.length());
    //System.out.println("path: " + path);
    int i0 = path.indexOf("/", 1);
    if(i0 < 0) {
      action = path.substring(1);
    } else {
      action = path.substring(1, i0);
      int i1 = path.indexOf("/", i0 + 1);
      id = getLong(path.substring(i0 + 1));
    }
    vctx.setLocale(request.getLocale());
    vctx.setAction(action);
    vctx.setAttribute(ForumConstants.ID_KEY, id);
    //System.out.println("action: " + action + " id: " + id);
    //vctx.setAttribute(ForumConstants.QUERYPARAMETERS_KEY, new HashMap(request.getParameterMap()));
    return vctx;
  }

  public String toURLString(ViewContext v, Map extraparams) {
    StringBuffer sbuf = new StringBuffer()
      .append(request.getContextPath())
      .append(ForumLocal.getForumEngine().getProperty(ForumConstants.SERVLET_MAPPING_PREFIX_KEY))
      .append("/")
      .append(v.getAction());
    Long id = (Long)v.getAttribute(ForumConstants.ID_KEY);
    if(id != null) {
      sbuf.append("/").append(id);
    }
    
    Map m = extraparams;
    if(m != null && m.size() > 0) {
      sbuf.append("?1=1");
      WebUtils.appendQueryParameters(sbuf, m);
    }
    return sbuf.toString();
  }

  //handle NumberFormatException gracefully
  private Long getLong(String s) throws Exception {
    return new Long(s);
    //try {
    //  return new Long(s);
    //} catch(NumberFormatException nfe) { }
    //return ForumConstants.LONG_1;
  }

}    
  
